package com.zhengqing.softdemo.config

/*
* import com.example.demo.entity.Account
import com.example.demo.entity.Card
import com.example.demo.keygen.HashCodeKeyGenerator
import com.example.demo.listener.LoggerListener
import com.example.demo.mask.IdNumberMaskProcessor
import com.example.demo.tenant.TenantIdFactory
* */
import com.zhengqing.softdemo.entity.Soft
import com.zhengqing.softdemo.keygen.HashCodeKeyGenerator
import com.zhengqing.softdemo.listener.LoggerListener
import com.zhengqing.softdemo.mask.IdNumberMaskProcessor
import com.zhengqing.softdemo.tenant.TenantIdFactory
import com.mybatisflex.annotation.KeyType
import com.mybatisflex.core.FlexGlobalConfig
import com.mybatisflex.core.FlexGlobalConfig.KeyConfig
import com.mybatisflex.core.keygen.KeyGeneratorFactory
import com.mybatisflex.core.keygen.KeyGenerators
import com.mybatisflex.core.keygen.impl.SnowFlakeIDKeyGenerator
import com.mybatisflex.core.logicdelete.LogicDeleteManager
import com.mybatisflex.core.logicdelete.impl.IntegerLogicDeleteProcessor
import com.mybatisflex.core.mask.MaskManager
import com.mybatisflex.core.mybatis.FlexConfiguration
import com.mybatisflex.core.tenant.TenantManager
import com.mybatisflex.spring.boot.ConfigurationCustomizer
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer
import com.mybatisflex.spring.boot.SqlSessionFactoryBeanCustomizer
import org.apache.ibatis.logging.stdout.StdOutImpl
import org.mybatis.spring.SqlSessionFactoryBean
import org.mybatis.spring.annotation.MapperScan
import org.springframework.context.annotation.Configuration

@Configuration // 扫描mapper接口所在的包
@MapperScan("com.zhengqing.softdemo.mapper")
class MybatisFlexConfig : ConfigurationCustomizer, SqlSessionFactoryBeanCustomizer, MyBatisFlexCustomizer {
    override fun customize(flexConfiguration: FlexConfiguration) {
        // 控制台显示日志
        flexConfiguration.logImpl = StdOutImpl::class.java
        println("Mybatis Configuration 配置：")
    }

    override fun customize(sqlSessionFactoryBean: SqlSessionFactoryBean) {
        println("SqlSessionFactoryBean 配置：")
    }

    override fun customize(globalConfig: FlexGlobalConfig) {
        println("Mybatis Flex配置：")
        // 全局配置监听器
        val loggerListener = LoggerListener()
        // globalConfig.registerInsertListener(loggerListener, Account::class.java, Card::class.java)
        // globalConfig.registerUpdateListener(loggerListener, Account::class.java, Card::class.java)
        globalConfig.registerInsertListener(loggerListener, Soft::class.java)
        globalConfig.registerUpdateListener(loggerListener, Soft::class.java)

        // Order // 全局配置id，实体类也可以重新配，不影响，比如Account
        val keyConfig = KeyConfig()
        keyConfig.keyType = KeyType.Generator
        keyConfig.value = KeyGenerators.flexId
        keyConfig.isBefore = true
        globalConfig.keyConfig = keyConfig

        // 自定义主键生成器
        KeyGeneratorFactory.register("hashcode", HashCodeKeyGenerator())
        // 雪花算法，分布式会用到 // 覆盖 KeyGenerators.snowFlakeId 加入地址等等生成
        KeyGeneratorFactory.register(KeyGenerators.snowFlakeId, SnowFlakeIDKeyGenerator(1, 1))

        // 逻辑删除值自定义设置
        // 默认0
        globalConfig.normalValueOfLogicDelete = 0
        // 默认1
        globalConfig.deletedValueOfLogicDelete = 2

        // 内置逻辑删除处理器 // 设置后上面的0，2设置就没用了！
        LogicDeleteManager.setProcessor(IntegerLogicDeleteProcessor())

        // 多租户 配置
        TenantManager.setTenantFactory(TenantIdFactory())

        // 自定义数据脱敏配置
        MaskManager.registerMaskProcessor("idNumber", IdNumberMaskProcessor())
    }
}